﻿#region license
// Copyright (c) 2009 Mauricio Scheffer
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//      http://www.apache.org/licenses/LICENSE-2.0
//  
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

using System;
using System.IO;
using System.Web;
using Castle.Core;
using Castle.Facilities.FactorySupport;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using log4net;
using log4net.Config;

namespace HttpModuleInjection {
    public class Global : HttpApplication, IContainerAccessor {
        public override void Init() {
            GlobalLog.Debug("HttpApplication init");
            base.Init();
        }

        public override void Dispose() {
            GlobalLog.Debug("HttpApplication dispose");
            base.Dispose();
        }

        protected void Application_Start(object sender, EventArgs e) {
            XmlConfigurator.ConfigureAndWatch(new FileInfo(Server.MapPath("/log4net.config")));
            GlobalLog = LogManager.GetLogger(typeof (Global));
            GlobalLog.Debug("Application_Start");
            container = new WindsorContainer();
            container.AddComponentLifeStyle<Service>(LifestyleType.Transient);
            container.AddFacility<FactorySupportFacility>();
            container.Register(Component.For<IHttpModule>()
                                   .ImplementedBy<UserHttpModule>()
                                   .LifeStyle.Custom<PerHttpApplicationLifestyleManager>());
            container.Register(Component.For<IHttpModule>()
                                   .ImplementedBy<UserHttpModule2>()
                                   .LifeStyle.Custom<PerHttpApplicationLifestyleManager>());

            container.Register(Component.For<HttpRequestBase>()
                .LifeStyle.PerWebRequest
                .UsingFactoryMethod(() => new HttpRequestWrapper(HttpContext.Current.Request)));

            container.Register(Component.For<HttpContextBase>()
                .LifeStyle.PerWebRequest
                .UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));

        }

        private static IWindsorContainer container;

        private static ILog GlobalLog;

        protected void Session_Start(object sender, EventArgs e) {}

        protected void Application_BeginRequest(object sender, EventArgs e) {}

        protected void Application_AuthenticateRequest(object sender, EventArgs e) {}

        protected void Application_Error(object sender, EventArgs e) {}

        protected void Session_End(object sender, EventArgs e) {}

        protected void Application_End(object sender, EventArgs e) {}

        public IWindsorContainer Container {
            get { return container; }
        }
    }
}